package sostats.core.base;

import java.io.IOException;
import java.util.Iterator;

import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;

import sostats.core.datasource.Configuration;
import sostats.core.help.ConfigurationHelper;
import sostats.core.help.SostatsException;

public class ContextFactory {
	@SuppressWarnings("unchecked")
	public static StatisticalContext newDefaultInstance() throws SostatsException{
		try {
			Configuration configuration = ConfigurationHelper.readProperites("config.properties");
			StatisticalContext context = new StatisticalContext(configuration);
			SAXReader reader = new SAXReader();
			Document doc = reader.read(StatisticalContext.class.getClassLoader().getResource("StatisticsContext.xml"));
			Element root = doc.getRootElement();
			Iterator<Element> tableIterator = root.elementIterator("table");
			while(tableIterator.hasNext()){
				Element tableElement = tableIterator.next();
				context.addTable(tableElement.attributeValue("name"));
				context.setTablePK(tableElement.attributeValue("name"),
						tableElement.attributeValue("distinctBy"));
				context.registryMetadata(
						tableElement.attributeValue("name"), 
						tableElement.attributeValue("description"));
				Iterator<Element> columnIterator = tableElement.elementIterator("column");
				while(columnIterator.hasNext()){
					Element columnElement = columnIterator.next();
					if(columnElement.attributeValue("type").equals("summary")){
						context.addSummary(
								columnElement.attributeValue("name"), 
								tableElement.attributeValue("name"));
						context.registryMetadata(
								tableElement.attributeValue("name")
								+StatisticalContext.CONNECTOR+columnElement.attributeValue("name"),
								columnElement.attributeValue("description"));
					}else if(columnElement.attributeValue("type").equals("category")){
						context.addCategory(
								columnElement.attributeValue("name"), 
								tableElement.attributeValue("name"));
						context.registryMetadata(
								tableElement.attributeValue("name")
								+StatisticalContext.CONNECTOR+columnElement.attributeValue("name"),
								columnElement.attributeValue("description"));
					}else {
						context.addCondition(
								columnElement.attributeValue("name"), 
								tableElement.attributeValue("name"));
						context.registryMetadata(
								tableElement.attributeValue("name")
								+StatisticalContext.CONNECTOR+columnElement.attributeValue("name"),
								columnElement.attributeValue("description"));
					}
				}
			}
			tableIterator=null;
			Iterator<Element> joinIterator = root.elementIterator("table-join");
			while(joinIterator.hasNext()){
				Element element = joinIterator.next();
				Element joined = element.element("joined");
				Element joining = element.element("joining");
				context.addJoint(
						joined.attributeValue("table"), 
						joined.attributeValue("column"), 
						joining.attributeValue("table"), 
						joining.attributeValue("column"));
			}
			return context;
		} catch (IOException e) {
			throw new SostatsException(e);
		} catch (DocumentException e) {
			throw new SostatsException(e);
		}
	}
}
